package com.software.levelupandroid.mine.model;

import android.content.Context;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.software.levelupandroid.entity.Badge;
import com.software.levelupandroid.entity.Grade;
import com.software.levelupandroid.entity.Result;
import com.software.levelupandroid.entity.Task;
import com.software.levelupandroid.home.CallBack;
import com.software.levelupandroid.utils.OkhttpUtil;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * @Description 个人中心的model层
 * @Author LiangYaJie
 * @CreateTime 2024年06月11日 10:21:03
 */
public class MineRepository {

    private static MineRepository mineRepository;

    public static MineRepository getMineRepository(){
        if (mineRepository == null){
            mineRepository = new MineRepository();
        }
        return mineRepository;
    }

    /**
     * 获取等级列表
     * @param callBack
     * @param context
     */
    public void getAllGrade(CallBack<List<Grade>> callBack, Context context){

        Call call = OkhttpUtil.getRequest("grade/getAllGrade", context);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                e.printStackTrace();
                callBack.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String resp = response.body().string();

                Result<List<Grade>> result = convertJsonToGradeList(resp);

                if (result != null){
                    if (result.getCode() != null && result.getCode().equals("200")){
                        List<Grade> gradeList = result.getData();
                        callBack.onSuccess(gradeList);
                    }else {
                        callBack.onFailure("result.getCode==null||result.getCode.isNotEquals('200'): 获取等级列表失败");
                    }
                }else {
                    callBack.onFailure("result==null: 未成功获取数据");
                }

            }
        });
    }



    /**
     * 获取徽章列表
     * @param callBack
     * @param context
     */
    public void getAllBadge(CallBack<List<Badge>> callBack, Context context){
        Call call = OkhttpUtil.getRequest("badge/getAllBadge", context);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                e.printStackTrace();
                callBack.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String resp = response.body().string();

                Result<List<Badge>> result = convertJsonToBadgeList(resp);

                if (result != null){
                    if (result.getCode() != null && result.getCode().equals("200")){
                        List<Badge> badges = result.getData();
                        callBack.onSuccess(badges);
                    }else {
                        callBack.onFailure("result.getCode==null||result.getCode.isNotEquals('200'): 获取徽章列表失败");
                    }
                }else {
                    callBack.onFailure("result==null: 未成功获取数据");
                }

            }
        });
    }

    public void getBadgeByUserId(CallBack<List<Badge>> callBack, Context context){
        Call call = OkhttpUtil.getRequest("badge/getAllBadgeByUserId", context);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                e.printStackTrace();
                callBack.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String resp = response.body().string();

                Result<List<Badge>> result = convertJsonToBadgeList(resp);

                if (result != null){
                    if (result.getCode() != null && result.getCode().equals("200")){
                        List<Badge> badges = result.getData();
                        callBack.onSuccess(badges);
                    }else {
                        callBack.onFailure("result.getCode==null||result.getCode.isNotEquals('200'): 获取徽章列表失败");
                    }
                }else {
                    callBack.onFailure("result==null: 未成功获取数据");
                }

            }
        });
    }

    /**
     * 将json转换为Result<List<Grade>>类型
     * @param respJson
     * @return
     */
    public Result convertJsonToGradeList(String respJson){
        Gson gson = new Gson();
        Type type = new TypeToken<Result<List<Grade>>>() {}.getType();
        Result<List<Grade>> result = gson.fromJson(respJson, type);

        return result;
    }

    /**
     * 将json转换为Result<List<Badge>>类型
     * @param respJson
     * @return
     */
    public Result convertJsonToBadgeList(String respJson){
        Gson gson = new Gson();
        Type type = new TypeToken<Result<List<Badge>>>() {}.getType();
        Result<List<Badge>> result = gson.fromJson(respJson, type);

        return result;
    }

}
